<script setup>
import { Layout, Menu, Tooltip } from 'ant-design-vue'
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { MenuUnfoldOutlined, MenuFoldOutlined, GithubOutlined } from '@ant-design/icons-vue'
import ThemeSwitch from './components/ThemeSwitch.vue'
import MainNavigation from './components/MainNavigation.vue'
import { getRecordInfo } from './api/api'

const { Header, Content, Sider, Footer } = Layout
const router = useRouter()
const collapsed = ref(false)
const isDarkMode = ref(false)
// 确保themeTransition始终是ref对象
const themeTransition = ref(false)
const recordInfo = ref({})
const recordInfoLoading = ref(true)

// 切换侧边栏折叠状态
const toggleCollapsed = () => {
  collapsed.value = !collapsed.value
}

// 主题切换处理
const handleThemeToggle = (newDarkMode) => {
  // 存储到本地存储
  localStorage.setItem('darkMode', newDarkMode ? 'dark' : 'light')
  
  // 添加/移除暗黑模式类
  if (newDarkMode) {
    document.documentElement.classList.add('dark-mode')
  } else {
    document.documentElement.classList.remove('dark-mode')
  }
}

// 初始化暗黑模式状态
onMounted(() => {
  // 从本地存储读取暗黑模式状态
  const savedDarkMode = localStorage.getItem('darkMode')
  if (savedDarkMode === 'dark') {
    isDarkMode.value = true
    document.documentElement.classList.add('dark-mode')
  } else {
    isDarkMode.value = false
    document.documentElement.classList.remove('dark-mode')
  }
  
  // 监听系统主题偏好变化
  const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
  const handleChange = (e) => {
    if (localStorage.getItem('darkMode') === null) {
      isDarkMode.value = e.matches
      document.documentElement.classList.toggle('dark-mode', e.matches)
    }
  }
  mediaQuery.addEventListener('change', handleChange)
  
  // 清理函数
  onUnmounted(() => {
    mediaQuery.removeEventListener('change', handleChange)
  })
  
  // 获取备案信息
  const loadRecordInfo = async () => {
    recordInfoLoading.value = true
    try {
      const data = await getRecordInfo()
      if (data && data.recordInfo) {
        recordInfo.value = data.recordInfo
      }
    } catch (error) {
      console.error('获取备案信息失败:', error)
    } finally {
      recordInfoLoading.value = false
    }
  }
  
  // 初始化时加载备案信息
  loadRecordInfo()
})

// 监听主题变化，添加全局过渡类
watch(isDarkMode, () => {
  document.documentElement.classList.toggle('theme-transition', themeTransition.value)
})
</script>

<template>
  <!-- 修复: 确保使用正确的ref对象访问方式 -->
  <Layout :class="{ 'theme-transition': themeTransition }" style="min-height: 100vh; overflow: hidden;">
    <!-- 侧边栏 -->
    <Sider width="200" :collapsed="collapsed" collapsible :trigger="null" style="background-color: #fff"
      class="custom-sider">
      <div class="logo" :class="{ 'collapsed': collapsed }">
        <span class="logo-text">Gulu Blog</span>
      </div>
      <MainNavigation :collapsed="collapsed" />
    </Sider>
    <!-- 主内容区域 -->
    <Layout class="main-layout">
      <!-- 顶部导航栏 -->
      <Header
        :style="{ padding: '0 24px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', boxShadow: '0 2px 8px rgba(0, 0, 0, 0.08)' }"
        :class="{ 'sider-collapsed': collapsed }">
        <div class="header-left">
          <button type="button" @click="toggleCollapsed" class="menu-toggle">
            <template v-if="collapsed">
              <MenuUnfoldOutlined />
            </template>
            <template v-else>
              <MenuFoldOutlined />
            </template>
          </button>
          <div class="header-title">
            <span class="title-text">Gulu Blog</span>
          </div>
        </div>
        <div class="header-right">
          <ThemeSwitch :isDarkMode="isDarkMode" :themeTransition="themeTransition"
            @update:isDarkMode="(val) => isDarkMode = val" @update:themeTransition="(val) => themeTransition = val"
            @toggle="(val) => handleThemeToggle(val)" />
        </div>
      </Header>
      <!-- 内容区域 -->
      <Content class="main-content" :class="{ 'sider-collapsed': collapsed }">
        <router-view />
      </Content>
      
      <!-- 底部备案信息区域 -->
      <Footer class="footer" :class="{ 'sider-collapsed': collapsed }">
        <div v-if="!recordInfoLoading && recordInfo" class="record-info-container">
          <div class="record-info">
            <template v-if="recordInfo.isShowIcp">
              <a :href="recordInfo.icpLink" target="_blank" rel="noopener noreferrer" class="record-link">
                {{ recordInfo.icp }}
              </a>
            </template>
            
            <template v-if="recordInfo.isShowIcp && (recordInfo.isShowBeian || recordInfo.isShowCompany)">
              <span class="record-separator">|</span>
            </template>
            
            <template v-if="recordInfo.isShowBeian">
              <a :href="recordInfo.beianLink" target="_blank" rel="noopener noreferrer" class="record-link">
                {{ recordInfo.beian }}
              </a>
            </template>
            
            <template v-if="recordInfo.isShowBeian && recordInfo.isShowCompany">
              <span class="record-separator">|</span>
            </template>
            
            <template v-if="recordInfo.isShowCompany">
              <span class="company-name">{{ recordInfo.company }}</span>
            </template>
          </div>
          
          <template v-if="recordInfo.isShowCopyright">
            <div class="copyright">
              {{ recordInfo.copyright }}
            </div>
          </template>
        </div>
      </Footer>
    </Layout>
  </Layout>
</template>

<style scoped lang="less">
/* 侧边栏样式 */
.logo {
  padding: 20px;
  text-align: center;
  transition: all 0.3s ease;
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  color: #1890ff;
  background: linear-gradient(135deg, #1890ff 0%, #722ed1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  display: inline-block;
}

.logo.collapsed .logo-text {
  font-size: 18px;
}

/* 确保侧边栏固定且不被顶部栏遮挡 */
.custom-sider {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1001;
  height: 100vh;
  background-color: #fff !important;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

/* 顶部导航栏样式 */
.ant-layout-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 1000;
  padding-left: 224px !important;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

/* 侧边栏收缩时的顶部导航栏样式 */
.ant-layout-header.sider-collapsed {
  padding-left: 84px !important;
}

.header-left {
  display: flex;
  align-items: center;
  margin-left: 0;
  padding-left: 0;
}

.menu-toggle {
  margin-right: 16px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: #666;
  padding: 0 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.menu-toggle:hover {
  background-color: #f5f5f5;
  color: #1890ff;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
  transition: all 0.3s ease;
}

.title-text {
  background: linear-gradient(135deg, #1890ff 0%, #722ed1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  gap: 16px;
  align-items: center;
}

.social-icon {
  color: #666;
  font-size: 20px;
  padding: 0 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.social-icon:hover {
  color: #1890ff;
  background-color: #f0f7ff;
}

/* 主内容区域样式 */
.main-content {
  padding: 24px;
  margin-top: 64px;
  margin-left: 200px;
  min-height: calc(100vh - 128px);
  transition: all 0.3s ease;
}

/* 侧边栏收缩时的主内容区域样式 */
.main-content.sider-collapsed {
  margin-left: 64px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .ant-layout-header {
    padding-left: 64px !important;
  }
  
  .ant-layout-header.sider-collapsed {
    padding-left: 64px !important;
  }
  
  .main-content,
  .main-content.sider-collapsed {
    margin-left: 64px;
  }
  
  .footer,
  .footer.sider-collapsed {
    margin-left: 64px;
  }
  
  .header-title {
    display: none;
  }
  
  /* 移动端备案信息样式调整 */
  .record-info {
    flex-direction: column;
    gap: 4px;
  }
  
  .record-separator {
    display: none;
  }
}

/* 主题切换动画 */
.theme-transition {
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

/* 底部备案信息样式 */
.footer {
  padding: 24px 24px;
  margin-left: 200px;
  text-align: center;
  background-color: #fff;
  border-top: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

/* 侧边栏收缩时的底部样式 */
.footer.sider-collapsed {
  margin-left: 64px;
}

.record-info-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.record-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.record-link {
  color: #666;
  text-decoration: none;
  transition: color 0.3s ease;
}

.record-link:hover {
  color: #1890ff;
  text-decoration: underline;
}

.record-separator {
  color: #d9d9d9;
}

.company-name {
  color: #666;
}

.copyright {
  color: #999;
  font-size: 12px;
}

/* 暗黑模式样式 */
:global(.dark-mode) {
  /* 侧边栏暗黑模式 */
  .custom-sider {
    background-color: #1f1f1f !important;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.3);
  }
  
  .logo-text {
    color: #ffffff !important;
    background: none !important;
    -webkit-text-fill-color: #ffffff !important;
  }
  
  /* 顶部导航栏暗黑模式 */
  .ant-layout-header {
    background-color: #1f1f1f !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
    border-bottom: 1px solid #2d2d2d !important;
  }
  
  /* 底部备案信息暗黑模式 */
  .footer {
    background-color: #1f1f1f !important;
    border-top: 1px solid #2d2d2d !important;
  }
  
  .record-link {
    color: #cccccc !important;
    
    &:hover {
      color: #1890ff !important;
    }
  }
  
  .company-name {
    color: #cccccc !important;
  }
  
  .copyright {
    color: #999999 !important;
  }
  
  .menu-toggle {
    color: #cccccc !important;
    
    &:hover {
      background-color: #2d2d2d !important;
      color: #1890ff !important;
    }
  }
  
  .title-text {
    color: #ffffff !important;
    background: none !important;
    -webkit-text-fill-color: #ffffff !important;
  }
  
  .social-icon {
    color: #cccccc !important;
    
    &:hover {
      color: #1890ff !important;
      background-color: #2d2d2d !important;
    }
  }
}
</style>
